//
//  PendApprovalInfoViewController.m
//  ZJControlsClass
//
//  Created by apple on 2024/11/1.
//

#import "PendApprovalInfoViewController.h"
#import "PendApprovalInfoBasicViewController.h"
#import "PendApprovalInfoSPInfoMationViewController.h"
#import "ZJPendApprovallNetWorking.h"
#import "ZJPendApprovallInfoModel.h"
#import "PendApprovallCanleAlertView.h"

#import "PendChildCollectionViewCell.h"
#import "PendApprovalShowPassFormViewController.h"

@interface PendApprovalInfoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UILabel * personLabel;
@property (nonatomic,strong) UILabel * timeLabel;
@property (nonatomic,strong) UIView * follewView;

/* 子视图*/
@property (nonatomic,strong) UICollectionView * tagCollectionView;
@property (nonatomic,strong) NSMutableArray * tagArr;
@property (nonatomic,strong) NSMutableArray * tagSelectArr;
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSArray * childDataSource;
@property (nonatomic,strong) UIView * v;
@property (nonatomic,strong) UIButton * canleBtn;
@property (nonatomic,strong) UIButton * agreeBtn;

@property (nonatomic,strong) ZJPendApprovallDoListInfoNetWorking * manager;
@property (nonatomic,strong) ZJPendApprovallInfoModel * model;
@property (nonatomic,strong) NSMutableDictionary * paramer;
@property (nonatomic,strong) PendApprovalInfoSPInfoMationViewController * spViewController;
@property (nonatomic,strong) PendApprovalInfoBasicViewController * BaseViewController;

/* alert */
@property (nonatomic,strong) PendApprovallCanleAlertView * alertView;
@property (nonatomic,strong) ZJPendApprovallInfoRefuseNetWorking * lInfoRefusemanager;
@property (nonatomic,strong) ZJPendApprovallInfoAgreeNetWorking * agreemanager;



@end

@implementation PendApprovalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"审批详情";
    
    [self loadNet];
    self.tagArr = [NSMutableArray arrayWithObjects:@"基本信息",@"审批信息", nil];
    self.tagSelectArr = [NSMutableArray arrayWithObjects:@"1",@"0", nil];
    
    
}



-(void)setShowOrHidden:(BOOL)showOrHidden{
    
    [self setUpUI];
    
    _showOrHidden = showOrHidden;
    if (showOrHidden){ //隐藏
        
        self.canleBtn.hidden = YES;
        self.agreeBtn.hidden = YES;
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.follewView.mas_bottom);
        }];
        
    }else{// 显示
        self.canleBtn.hidden = NO;
        self.agreeBtn.hidden = NO;
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.follewView).with.offset(-ALIGN_HEIGHT_PT(100));
        }];
    }
    
}




#pragma mark -- 网络请求
-(void)loadNet{
    
    [self.paramer setValue:self.taskId forKey:@"taskId"];
    [self.paramer setValue:self.processInstanceId forKey:@"processInstanceId"];
    [self.manager dispatchTaskWithParam:self.paramer completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {
            
            self.model = [ZJPendApprovallInfoModel mj_objectWithKeyValues:responseObject[@"data"]];
            
            self.BaseViewController.model = self.model;
            self.spViewController.model = self.model;
            
            
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
            
            }];
        }
    }];
    
}
-(void)setTitleStr:(NSString *)titleStr{
    self.titleLabel.text = titleStr;
}

-(void)setPersonStr:(NSString *)personStr{
    self.personLabel.text = [NSString stringWithFormat:@"申请人员：%@",personStr];
}
-(void)setTimeStr:(NSString *)timeStr{
    self.timeLabel.text = [NSString stringWithFormat:@"申请时间：%@",timeStr];
}




-(void)setUpUI{

    UIImageView * img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150 + StatusBarHeight)];
    img.image = [UIImage imageNamed:@"Mask group-1"];
    [self.view addSubview:img];
    
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.personLabel];
    [self.view addSubview:self.timeLabel];
    [self.view addSubview:self.follewView];
    [self.follewView addSubview:self.tagCollectionView];
    [self.follewView addSubview:self.collectionView];
    [self.follewView addSubview:self.v];
    [self.view addSubview:self.canleBtn];
    [self.view addSubview:self.agreeBtn];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.view).with.offset(StatusBarHeight + ALIGN_HEIGHT_PT(18));
        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(12));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(25)));
    }];
    
    [self.personLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(12));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(20)));
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(12));
        make.top.equalTo(self.personLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(5));
        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(12));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(20)));
    }];
    
    [self.follewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).with.offset(ALIGN_HEIGHT_PT(130) + StatusBarHeight);
        make.bottom.equalTo(self.view).with.offset(ALIGN_HEIGHT_PT(20));
    }];
    
    [self.tagCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.follewView);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
    }];
        
    [self.v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.follewView);
        make.top.equalTo(self.tagCollectionView.mas_bottom);
        make.height.equalTo(@(1));
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.follewView.mas_bottom).with.offset(-ALIGN_HEIGHT_PT(100));
        make.top.equalTo(self.tagCollectionView.mas_bottom).with.offset(1);
    }];
    
    [self.canleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom).with.offset(ALIGN_HEIGHT_PT(10));
        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(10));
        make.size.equalTo(@(CGSizeMake((SCREEN_WIDTH - ALIGN_WIDTH_PT(30)) / 2, ALIGN_HEIGHT_PT(45))));
    }];
    
    [self.agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.canleBtn.mas_centerY);
        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(10));
        make.size.equalTo(@(CGSizeMake((SCREEN_WIDTH - ALIGN_WIDTH_PT(30)) / 2, ALIGN_HEIGHT_PT(45))));
    }];
    
}


#pragma mark -- 点击事件
-(void)respondsTo_canleBtn{ // 驳回
    
    __weak typeof (self)weakSelf = self;
    weakSelf.alertView = [PendApprovallCanleAlertView new];
    weakSelf.alertView.typeStr = @"确认驳回";
    weakSelf.alertView.block = ^(NSString * msg) {
        
        if (msg.length == 0) {
            [MBProgressHUD showError:@"请输入驳回理由"];
            return;
        }
        
        
        

        NSDictionary * dic = @{@"taskId":self.taskId,@"comments":msg};
        [self.lInfoRefusemanager dispatchTaskWithParam:dic completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (responseObject && [responseObject[@"code"] intValue] == 200){
                [MBProgressHUD showSuccess:@"驳回成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
            }
        }];

    };
    
    [kWindow addSubview:self.alertView];
    
}

-(void)respondsTo_agreeBtn{// 通过
    
    if ([self.model.currentNode.type isEqualToString:@"FORM"]){

        PendApprovalShowPassFormViewController * vc = [PendApprovalShowPassFormViewController new];
        vc.formItems = self.model.currentNode.formItems;
        vc.taskId = self.taskId;
        [self.navigationController presentViewController:vc animated:YES completion:nil];
        
    }else{
        __weak typeof (self)weakSelf = self;
        weakSelf.alertView = [PendApprovallCanleAlertView new];
        weakSelf.alertView.typeStr = @"确认通过";
        weakSelf.alertView.block = ^(NSString * msg) {

            NSDictionary * dic = @{@"taskId":self.taskId,@"comments":msg};
            [self.agreemanager dispatchTaskWithParam:dic completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                if (responseObject && [responseObject[@"code"] intValue] == 200){
                    [MBProgressHUD showSuccess:@"通过成功"];
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
                }
            }];

        };
        
        [kWindow addSubview:self.alertView];
    }
    
}


#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.tagCollectionView]) {
        return self.tagArr.count;
    }
    return self.childDataSource.count;
}


-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([collectionView isEqual:self.tagCollectionView]) {
        PendChildCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PendChildCollectionViewCell" forIndexPath:indexPath];
        
        if ([self.tagSelectArr[indexPath.row] isEqualToString:@"1"]) {
            cell.follewView.backgroundColor = COLOR_RGB(50, 113, 237);
            cell.label.textColor = COLOR_RGB(50, 113, 237);
        }else{
            cell.follewView.backgroundColor = [UIColor clearColor];
            cell.label.textColor = COLOR_RGB(102, 102, 102);
        }
        cell.label.text = [NSString stringWithFormat:@"%@",self.tagArr[indexPath.row]];
        
        return cell;
    }else{
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
        UIViewController *vc = self.childDataSource[indexPath.row];
            [cell.contentView addSubview:vc.view];
            [vc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.top.equalTo(cell.contentView);
            }];
        return cell;
        
    }
    return nil;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([collectionView isEqual:self.tagCollectionView]) {
        
        CGFloat offsetX = indexPath.row * SCREEN_WIDTH;
        [self.collectionView setContentOffset:CGPointMake(offsetX, 0)];
        
        [self.tagSelectArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.tagSelectArr replaceObjectAtIndex:idx withObject:@"0"];
        }];
        [self.tagSelectArr replaceObjectAtIndex:indexPath.row withObject:@"1"];
        [self.tagCollectionView reloadData];
        
    }
}





#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual: self.tagCollectionView]) {
        return CGSizeMake(SCREEN_WIDTH / 2, ALIGN_HEIGHT_PT(45));
    }else{
        return self.collectionView.bounds.size;
    }
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(0);
}
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(0);
}





#pragma mark -- 懒加载
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = FONT16;
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}

-(UILabel *)personLabel{
    if (!_personLabel) {
        _personLabel = [UILabel new];
        _personLabel.textAlignment = NSTextAlignmentLeft;
        _personLabel.font = FONT12;
        _personLabel.textColor = [UIColor blackColor];
        _personLabel.text = @"申请人员：张三";
    }
    return _personLabel;
}

-(UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.font = FONT12;
        _timeLabel.textColor = [UIColor blackColor];
        _timeLabel.text = @"申请时间：2024-10-02 14:56:32";
    }
    return _timeLabel;
}

-(UIView *)follewView{
    if (!_follewView) {
        _follewView = [UIView new];
        _follewView.backgroundColor = [UIColor whiteColor];
        _follewView.layer.cornerRadius = 20;
        _follewView.layer.masksToBounds = YES;
    }
    return _follewView;
}


-(UICollectionView *)tagCollectionView{
    if (!_tagCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _tagCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _tagCollectionView.delegate = self;
        _tagCollectionView.dataSource = self;
        _tagCollectionView.userInteractionEnabled = YES;
        _tagCollectionView.backgroundColor = [UIColor clearColor];
        _tagCollectionView.showsVerticalScrollIndicator = false;
        _tagCollectionView.showsHorizontalScrollIndicator = false;
        
        // cell
        [_tagCollectionView registerClass:[PendChildCollectionViewCell class] forCellWithReuseIdentifier:@"PendChildCollectionViewCell"];
    }
    return _tagCollectionView;
}



- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.scrollEnabled = NO;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        _collectionView.backgroundColor = [UIColor clearColor];
    }
    return _collectionView;
}

- (NSArray *)childDataSource{
    if (!_childDataSource) {
        
        
        [self addChildViewController:self.BaseViewController];
        
        [self addChildViewController:self.spViewController];
        
        _childDataSource = @[self.BaseViewController, self.spViewController];
        
    }
    return _childDataSource;
}

-(UIView *)v{
    if (!_v) {
        _v = [UIView new];
        _v.backgroundColor = COLOR_RGB(224, 224, 224);
    }
    return _v;
}



-(UIButton *)canleBtn{
    if (!_canleBtn) {
        _canleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_canleBtn setTitle:@"拒绝" forState:UIControlStateNormal];
        [_canleBtn setTitleColor:COLOR_RGB(50, 113, 237) forState:UIControlStateNormal];
        _canleBtn.titleLabel.font = FONT14;
        _canleBtn.layer.cornerRadius = 5;
        _canleBtn.layer.masksToBounds = YES;
        _canleBtn.layer.borderColor = COLOR_RGB(50, 113, 237).CGColor;
        _canleBtn.layer.borderWidth = 0.5;
        [_canleBtn addTarget:self action:@selector(respondsTo_canleBtn) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _canleBtn;
}

-(UIButton *)agreeBtn{
    if (!_agreeBtn) {
        _agreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_agreeBtn setTitle:@"同意" forState:UIControlStateNormal];
        [_agreeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _agreeBtn.titleLabel.font = FONT14;
        _agreeBtn.layer.cornerRadius = 5;
        _agreeBtn.layer.masksToBounds = YES;
        _agreeBtn.backgroundColor = COLOR_RGB(50, 113, 237);
        [_agreeBtn addTarget:self action:@selector(respondsTo_agreeBtn) forControlEvents:(UIControlEventTouchUpInside)];

    }
    return _agreeBtn;
}

-(ZJPendApprovallDoListInfoNetWorking *)manager{
    if (!_manager) {
        _manager = [ZJPendApprovallDoListInfoNetWorking new];
    }
    return _manager;
}
-(NSMutableDictionary *)paramer{
    if (!_paramer) {
        _paramer = [NSMutableDictionary dictionary];
    }
    return _paramer;
}

-(PendApprovalInfoBasicViewController *)BaseViewController{
    if (!_BaseViewController) {
        _BaseViewController = [PendApprovalInfoBasicViewController new];
    }
    return _BaseViewController;
}


-(PendApprovalInfoSPInfoMationViewController *)spViewController{
    if (!_spViewController) {
        _spViewController = [PendApprovalInfoSPInfoMationViewController new];
    }
    return _spViewController;
}


-(ZJPendApprovallInfoRefuseNetWorking *)lInfoRefusemanager{
    if (!_lInfoRefusemanager){
        _lInfoRefusemanager = [ZJPendApprovallInfoRefuseNetWorking new];
    }
    return _lInfoRefusemanager;
}
-(ZJPendApprovallInfoAgreeNetWorking *)agreemanager{
    if (!_agreemanager){
        _agreemanager = [ZJPendApprovallInfoAgreeNetWorking new];
    }
    return _agreemanager;
}


@end
